package com.weiyun.shortlink.admin.controller;

import com.weiyun.shortlink.admin.common.convention.result.Result;
import com.weiyun.shortlink.admin.common.convention.result.Results;
import com.weiyun.shortlink.admin.dto.req.UserLoginReqDto;
import com.weiyun.shortlink.admin.dto.req.UserRegistryReqDto;
import com.weiyun.shortlink.admin.dto.req.UserUpdateReqDto;
import com.weiyun.shortlink.admin.dto.resp.UserLoginResponseDto;
import com.weiyun.shortlink.admin.dto.resp.UserResponseDTO;
import com.weiyun.shortlink.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/***
 *   @Author:weiyun
 *   用户管理控制层
 */
@RestController
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;

    /**
     * @param username 根据用户名查询用户信息
     */
    @GetMapping("/api/shortLink/admin/v1/user/{username}")
    public Result<UserResponseDTO> getUserByUsername(@PathVariable("username") String username) {
            return Results.success(userService.getUserByUserName(username) );
    }

    /**
     *
     * @param username 查询用户名是否存在
     *
     */
    @GetMapping("/api/shortLink/admin/v1/user/has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") String username){
        Boolean b = userService.hasUserName(username);
        return Results.success(!b);
    }
    @PostMapping("/api/shortLink/admin/v1/user")
    public Result<Void> register(@RequestBody UserRegistryReqDto registryReqDto){
        userService.Register(registryReqDto);
        return Results.success();
    }

    @PutMapping("/api/shortLink/admin/v1/user")
    public Result<Void> update(@RequestBody UserUpdateReqDto userUpdateReqDto){
        userService.updateFill(userUpdateReqDto);
        return Results.success();
    }
    @PostMapping("/api/shortLink/admin/v1/user/login")
    public Result<UserLoginResponseDto> login(@RequestBody UserLoginReqDto userLoginReqDto){
        UserLoginResponseDto userLoginResponseDto = userService.login(userLoginReqDto);
        return Results.success(userLoginResponseDto);
    }

    @GetMapping("/api/shortLink/admin/v1/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam("username") String username,@RequestParam("token") String token){
        return Results.success(userService.checkLogin(username,token));
    }

    @DeleteMapping("/api/shortLink/admin/v1/user/logout")
    public Result<Void> logout(@RequestParam("username") String username,@RequestParam("token") String token){
        userService.logout(username,token);
        return Results.success();
    }
}
